classdef uDwc < uController
    % uController class enables control of uController via USB.
    % udwc controls attenuators, CLK generators,  
    
    
    properties(Constant)
        EEPROM_BYTE_NUMBER = 4096;
        EEPROM_READ_ADDRESS = hex2dec('A1');
        EEPROM_WRITE_ADDRESS = hex2dec('A0');
        EXPANDER_READ_ADDRESS = hex2dec('7D');
        EXPANDER_WRITE_ADDRESS = hex2dec('7C');
        LTC2493_READ_ADDRESS = hex2dec('69');
        LTC2493_WRITE_ADDRESS = hex2dec('68');
    end
    
    methods(Static)
        function test()
            try
                uC = uController('COM4');
                % setting low attenuation
                uC.set_attenuation(2, 0);   % To uDWC dedicated file %
                
                % programming the buffer
                %uC.set_serial_buffer(0, [0 0 0 0 0 0 0 0]); % To uDWC dedicated file %
            catch error
                disp(error.identifier);
                disp(error.message);
                for s=1:length(error.stack)
                    disp(error.stack(s).name);
                    disp(error.stack(s).line);
                end
            end
            uC.delete();
        end
    end
        
    methods
        function this = uDwc()
        end
        
        function set_RF_channel(this, channel)  % To uDWC dedicated file %
            % used where the channels are changed manually
%             h = msgbox(sprintf('Change the RF channel to %d', channel),...
%                 'RF channel', 'warn');
%             uiwait(h);
        end
        
        function set_IF_channel(this, channel)  % To uDWC dedicated file %
%             h = msgbox(sprintf('Change the IF channel to %d', channel),...
%                 'IF channel', 'warn');
%             uiwait(h);
        end
        
        function set_attenuation(this, channel, att)    % To uDWC dedicated file %
            % Sets the attenuation of the specified channel. The
            % attenuation can be in range of 0 to 31 dB with a step of 0.5
            % dB.
            
            % settings for the AMC-TESt board
            att = round(63 - 2*att);
            if att < 1 || att > 63
                WrongAtt = MException('uController:set_attenuation','The set attenuation value is out of range');
                throw(WrongAtt);
            end
            if channel < 0 || channel > 9
                WrongChannel = MException('uController:set_attenuation',...
                    'The specified channel do not exist');
                throw(WrongChannel);
            end
%             sending the attenuation value
            this.set_pin_output('B07',0);
            this.SPI_write(att);
            
            bits = dec2bin(channel,4);
            % setting the specified attenuator
            this.set_pin_output('A03',str2double(bits(4)));
            this.set_pin_output('A00',str2double(bits(3)));
            this.set_pin_output('C03',str2double(bits(2)));
            this.set_pin_output('C02',str2double(bits(1)));

            this.set_pin_output('B07',1);
            this.set_pin_output('B07',0);
            % coming back to the initial state
            this.set_pin_output('A03',1);
            this.set_pin_output('A00',1);
            this.set_pin_output('C03',1);
            this.set_pin_output('C02',1);
            
            this.set_pin_output('B07',1);
            this.set_pin_output('B07',0);
              
        end
        
        function set_configuration(this, conf, RF_source, LO_source)
%             if strcmp(conf,'NWA')
%                 h = msgbox('Change the configuration to NWA (port 1 for RF, port 2 for LO)',...
%                     'RF channel', 'warn');
%                 uiwait(h);
%             elseif strcmp(conf,'SAN')
%                 h = msgbox('Change the configuration to signal analyzer (port 2)',...
%                     'RF channel', 'warn');
%                 uiwait(h);
%             end
        end
        
        function temperature = get_temperature(this) % To uDWC dedicated file %
            temperature = 0;
        end
        
        function LO_detection_state = get_LO_detection_state(this) % To uDWC dedicated file %
            LO_detection_state = 1;
        end
        
        function data = expanderReadByte(this)
            this.i2cWriteAddress(uDwc.EXPANDER_READ_ADDRESS);
            data = this.i2cReadAndStop();
        end
        
        function expanderWriteByte(this, data)
            this.i2cWriteAddress(uDwc.EXPANDER_WRITE_ADDRESS);
            this.i2cWriteAndStop(data);
        end
        
        
    end
end